$(".tabBarBox").load("../../common-html/resource-topTab.html");
var comPageNum = 0;//分页公共页码
var isEdit = false;//是否编辑--判断是否跟新页码到首页
var code=''
$("#form-member").validate({
    rules: {
        username:"titleCheck",
    },
    //提交表单数据
    submitHandler:function(form){
        queryDataFn();
    }
})
function queryDataFn() {
    var category_id=$("#deploy").val(),
        logmax=$("#logmax").val(),
        logmin=$("#logmin").val(),
        shaixuan=$("#shaixuan").val(),
        text=$("#text").val(),
        pingtai=$("#pingtai").find("option:selected").val(),
        cangku=code;
    console.log(category_id)
    //提交
    var user_data = {
        category_id:category_id,
        created_at_end:logmax,
        created_at_start:logmin,
        status:shaixuan,
        keyword:text,
        platform:pingtai,
        warehouse_code:cangku
    }
    console.log(user_data)
    //获取返回的数据
    var succCallBack = function (data, status, response) {
        if(data.errorCode=="0") {
            var dataSource = new Array();
            var table;   //table对象
            dataSource = [];
            var result=data.data
            var q=0
            $.each(result, function (i, field){
                q++
                var rowData = new Array();
                rowData.push(q);
                rowData.push(field.platform);
                rowData.push(field.warehouse_name);
                //活动标题-0
                rowData.push(field.keyword);
                //生效时间-1
                rowData.push(field.started_at);
                //失效时间-2
                rowData.push(field.end_in);
                //状态-3
                rowData.push(field.status);
                //创建人-4
                rowData.push(field.created_name);
                //创建时间-5
                rowData.push(field.created_at);
                //类目id-6
                rowData.push(field.category_id);
                //活动id-7
                rowData.push(field.resource_id);
                //url地址-8
                rowData.push(field.image_url);
                rowData.push(field.warehouse_code);
                dataSource.push(rowData);
            });
            initDataTable(dataSource);
            return false
        }else{
            //报错
            var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
            dttable.fnClearTable(); //清空一下table
            return false
        }
    };
    //给一个接口地址提交数据
    return web.doAjax(root.sroreinter.search_resource_conf, 'post', user_data, succCallBack);
    //获取数据之后如何展现在页面上
    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable( ); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "fnDrawCallback" : function(){
                if(!isEdit){
                    comPageNum = getDataTablePageNum(dttable);
                }
                this.api().column(0).nodes().each(function(cell,i){
                    cell.innerHTML = i+1;
                    //权限控制函数
                    power()
                });
            },
            "autoWidth": false,
            "order": [[ 8, "desc" ]],
            "ordering": true,
            "searching": true,
            "paging": true,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu":[10,20,30],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {"data":null,"targets":0 },
                {
                    "render": function(data,type,row,meta){
                        var val
                        if (row[1]=='10,20'){
                            val='普通平台/签约平台'
                        }else if (row[1]=='20'){
                            val='普通平台'
                        }else{
                            val='签约平台'
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function(data,type,row,meta){
                        var val ='<a class="powerButton 457"><i class="Hui-iconfont mr-10" onclick="edit_data(\''+row[3]+'\',\''+row[4]+'\',\''+row[5]+'\',\''+row[11]+'\','+row[9]+','+row[10]+')">&#xe6df;</i></a> <a class="powerButton 458"><i class="Hui-iconfont" onclick="delete_data('+row[10]+',$(this))">&#xe6e2;</i></a>';
                        return val;
                    },
                    "targets": 9
                },
            ]
        });
        dttable.fnPageChange(comPageNum);
        isEdit = false;
        //权限控制函数
        power()
    };
}
function changeCang() {
    code=''
    var warcode=""
    var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
    $.each(warehouses.data,function(k,s){
        if(document.getElementById('yun'+s.warehouseCode+'').checked==true){//获取选中的id
            warcode+=document.getElementById('yun'+s.warehouseCode+'').value+ ","//逗号拼接获取到的所以id
        }
    })
    //去掉最后一位的逗号
    if (warcode.length > 0) {
        warcode = warcode.substr(0, warcode.length - 1);
    }
    console.log(warcode)
    code=warcode

}
function delete_data(id,obj){
    layer.confirm('你确定要删除吗？',function(index){
        //此处请求后台程序，下方是成功后的前台处理……
        $.ajax({
            type: "post",
            url: root.sroreinter.delete_resource_conf,
            cache: false,  //禁用缓存
            dataType: "json",
            data: {
                resource_id:id,
            },
            //传入组装的参数
            success: function (data,user_data,succCallBack) {
                console.log(data)
                if(data.errorCode=="0") {
                    layer.msg('操作成功!', {icon: 6, time: 1000});
                    $(obj).parents("tr").remove();
                    setTimeout( function () {
                        isEdit = true;
                        queryDataFn();
                    },700);
                }

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest);
            }
        })
    });


}
function edit_data(keyword,started_at,end_in,image_url,category_id,resource_id){
    var index = layer.open({
        type: 2,
        title:"搜索栏编辑配置",
        content: "search_edit.html"+"?keyword="+keyword+"&started_at="+started_at+"&end_in="+end_in+"&category_id="+category_id+"&resource_id="+resource_id+"&image_url="+encodeURIComponent(encodeURIComponent(image_url))
    });
    layer.full(index);
}
// /*跳转页面查询出来的配置参数*/
// var path = window.location.href;
// var param = path.split("?")
// var i=param[1].split("=")
// var a=i[1]
// var q=decodeURI(a)
// var getValue =decodeURI(q)
/*
下拉框的展示1
 */
$(function () {
    $.ajax({
        type: "post",
        url: root.sroreinter.get_category,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            category_id:1,type:101
        },  //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter = data.data;
                var interText = doT.template($("#ascription_bob").text());
                // $("#category_id").html(interText(dataInter));
                $.each(dataInter, function (inx, data2) {
                    if (data2.name == "搜索栏配置") {
                        var member = '<option selected value="' + data2.category_id + '" >' + data2.name + '</option>';
                        $('#category_id').append(member)
                    } else {
                        var member = '<option value="' + data2.category_id + '" >' + data2.name + '</option>';
                        $('#category_id').append(member)
                    }
                })
            }
        }
    })
})
/*
下拉框的展示2
 */
$(function () {
    $.ajax({
        type: "post",
        url: root.sroreinter.get_category,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            category_id:8,type:101
        },  //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                var dataInter=data.data;
                var interText = doT.template($("#ascription_bobb").text());
                $("#deploy").html(interText(dataInter));
            }
        }
    })
})

function discount() {
    $("#category_id").change(function(event) {
        var fanx = $(this).find("option:selected");

        if(fanx.val()==3){
            window.location.href="../resource-html/resource.html?value="+"拉幕配置"
        }
        if(fanx.val()==4){
            window.location.href="../resource-html/resource.html?value="+"浮框配置"
        }
        if(fanx.val()==5){
            window.location.href="../resource-html/resource.html?value="+"弹框配置"
        }
        if(fanx.val()==2){
            window.location.href="../top-html/top.html"
        }
        //轮播图配置
        if(fanx.val()==6){
            window.location.href="../picture-html/picture.html"
        }
        //品类展示区配置
        if(fanx.val()==18){
            window.location.href="../display-html/display.html"
        }
        //楼层配置
        if(fanx.val()==10){
            // window.location.href="../floor-html/floor.html"
            window.location.href = "../floorManagement-html/floorManagement.html"
        }
        if(fanx.val()==21){
            window.location.href="../categorylogo-html/categorylogo.html"
        }
        if(fanx.val()==22){
            window.location.href="../brandhalllogo-html/brandhalllogo.html"
        }
    });
}